import tkinter as tk
from tkinter import ttk
import tkinter.font as tkfont


class TreeViewExample:
    def __init__(self,master=None,**kwargs):
        self.click_item=kwargs.get('click_item')
        self.root = master
        self.root.configure(width=200)

        # 创建一个Frame作为容器
        main_frame = tk.Frame(self.root, width=30, height=30)
        main_frame.place(relwidth=1,relheight=1)

        # 添加系统标题标签
        title_frame = tk.Frame(main_frame, background="#4a7a8c", height=300)
        title_frame.pack(fill='x', anchor='n')

        title_label = tk.Label(title_frame, text="XXXX系统",
                               font=('Arial', 20, 'bold'),
                               background="#4a7a8c", foreground="white",
                               padx=5, pady=5)
        title_label.pack(anchor="w")  # 左对齐

        # 创建自定义样式
        style = ttk.Style()
        style.theme_use("default")  # 可选值: 'clam', 'alt', 'default', 'classic'


        style.configure("Custom.Treeview", rowheight=30, activebackground="blue", foreground="green",font=("微软雅黑", 12))



        tree_frame = tk.Frame(main_frame)
        tree_frame.pack(expand=True, side=tk.LEFT,fill=tk.Y)


        # 创建Treeview控件 - 修改这里的show参数
        columns = ("name", "value")
        self.tree = ttk.Treeview(tree_frame, style="Custom.Treeview",
                                 selectmode=tk.BROWSE,
                                 # 关闭不显示列名
                                 # columns=columns,
                                 show="tree",  # 修改为"tree headings"

                                 )

        # self.tree.heading("name", text="名称")
        # self.tree.heading("value", text="值")
        #
        # # 可选：设置列宽
        # self.tree.column("name", width=100)
        # self.tree.column("value", width=100)

        self.tree.pack(side=tk.LEFT, expand=True, fill='both')


        # 添加一些示例数据
        self.populate_tree()

        # 添加按钮用于演示插入操作
        insert_btn = tk.Button(self.root, text="插入新节点", command=self.insert_item)
        insert_btn.pack(pady=5, anchor="n")

        self.tree.bind("<ButtonRelease-1>", lambda event: self.on_tree_click(event))

    def on_tree_click(self, event):
        # 获取点击的坐标
        x, y = event.x, event.y

        # 获取点击位置对应的项目
        item_id = self.tree.identify("item", x, y)

        # 获取点击的区域（可能是 tree, cell, column, row 等）
        region = self.tree.identify("region", x, y)

        # 如果点击的是一个项目
        if item_id:
            item_text = self.tree.item(item_id, "text")
            item_values = self.tree.item(item_id, "values")

            print(f"点击坐标: ({x}, {y})")
            print(f"点击区域: {region}")
            print(f"点击项目ID: {item_id}")
            print(f"项目文本: {item_text}")
            self.click_item.set(item_text)
            print(f"项目值: {item_values}")
            print("-" * 30)

    def populate_tree(self):
        # 添加根节点
        root_node = self.tree.insert("", "end", text="根节点", values=("root", "根值"))

        # 添加一些子节点
        self.tree.insert(root_node, "end", text="子节点1", values=("child1", "值1"))
        self.tree.insert(root_node, "end", text="子节点2", values=("child2", "值2"))

    def insert_item(self):
        selected = self.tree.selection()
        if selected:
            parent = selected[0]
        else:
            parent = ""

        new_item = self.tree.insert(parent, "end", text="新节点", values=("新名称", "新值"))
        print(f"已插入新节点: {new_item}")